module MC {
	export module Call {
		export class MsgWin {
			public constructor() {

				this.init();
			}
			/** 最新的最大id */
			private maxid = 0;
			/** 当前对象的用户id */
			private uid: any = 0;
			private target_id: any = 0;
			private pass: string;
			private init() {
				let mthis = this;
				this.target_id = 'mc_' + MC.get('uid');
				MC.user.getInfo(this.target_id, (json: any) => {
					//console.log(json);
					$('#titleName').text(json.name);
				});


				MC.Call.Jiguang((uid: string) => {
					this.uid = uid;
					this.onMsgReceive();
				});


				$('.setJobSend').click(() => {
					this.setJobSend();
				});

				MC.Call.File($(".upfilepicIco [type=file]"), (json: any) => {
					console.log(json);
					// 发送消息
					JIM.sendSinglePic({
						'target_username': this.target_id,
						'image': json
					}).onSuccess(function (data: any, msg: any) {
						console.log(data, msg);
						mthis.addli(msg.content, msg.msg_id);
						mthis.buttom();
						//data.code 返回码
						//data.message 描述
						//data.msg_id 发送成功后的消息id
						//data.ctime_ms 消息生成时间,毫秒
						//data.appkey 用户所属 appkey
						//data.target_username 用户名
						//msg.content 发送成功消息体
					}).onFail(function (data: any) {
						console.log(data);
						//同发送单聊文本
					});
				});


				$('#scrollDiv').on('click','img.msgPic',function(){
					let one=$(this).attr('src');
					let listobj=$('#scrollDiv img.msgPic');
					let list=[];
					for(let i=0;i<listobj.length;i++){
						list.push(listobj[i].src);
					}
					if(one!==''){
						MC.msg.Image(one,list);
					}
				});

			}


			/**  监听聊天实时信息 */
			private onMsgReceive() {
				JIM.onMsgReceive((data: any) => {
					//console.log('onMsgReceive', data);
					let messages = data.messages;
					for (let i = 0; i < messages.length; i++) {
						let one = messages[i];
						if (one.from_username == this.target_id) {
							this.addli(one.content, one.msg_id);
						}
					}
					this.buttom();

					//	setTimeout(() => {
					this.resetUnreadCount();
					//	}, 2000);
				});

				JIM.onSyncConversation((data: any) => {
					//	console.log("onSyncConversation", data);
					if (data.length > 0) {
						$('.callfirst').hide();
					}
					let uid = 'mc_' + MC.get('uid');
					for (let i = 0; i < data.length; i++) {
						let one = data[i];
						if (one.from_username == uid) {
							let messages = one.msgs;
							for (let i = 0; i < messages.length; i++) {
								let one = messages[i];
								this.addli(one.content, one.msg_id);
							}
						}
					}
					this.buttom();

				});
				setTimeout(() => {
					this.resetUnreadCount();
				}, 2000);

			}

			////设置当前聊天为已读了
			private resetUnreadCount() {
				JIM.resetUnreadCount({
					'username': this.target_id
				});
			}



			private setJobSend() {
				let mthis = this;
				MC.click(() => {

					let quest = $('#quest').val();
					if (quest == '') {
						MC.msg.alert("必须的输入内如");
						return;
					}
					if (quest.length > 140) {
						MC.msg.alert("输入的内容太多了");
						return;
					}
					$('#quest').val('');

					// 发送消息
					JIM.sendSingleMsg({
						'target_username': this.target_id,
						'content': quest,
					}).onSuccess(function (data: any, msg: any) {
						///console.log(data, msg);
						mthis.addli(msg.content, msg.msg_id);
						mthis.buttom();
						//data.code 返回码
						//data.message 描述
						//data.msg_id 发送成功后的消息 id
						//data.ctime_ms 消息生成时间,毫秒
						//data.appkey 用户所属 appkey
						//data.target_username 用户名
						//msg.content 发送成功消息体,见下面消息体详情
					}).onFail(function (data: any) {
						console.log(data);
					});





				});
			}
			/**
			 * 刷新消息
			 */
			private upmsg() {

			}



			private addli(content: any, id: string) {
				//	console.log(content);
				////	return;
				///isme:boolean,head:string,content:string,id:string
				let isme = this.uid == content.from_id;////
				let head = '';
				let li: any;
				let text = '';
				if (content.msg_type == 'text') {
					text = '<p>' + content.msg_body.text + '</p>';
				} else if (content.msg_type == 'image') {
					text = '<div class="msgPicDiv"><img media_id="' + content.msg_body.media_id + '" class="msgPic"></div>';
				} else {
					text = '<span class="titleSpan">不支持类型</span>';
				}

				let uid = content.from_id;
				if (isme) {
					li = $(`<div class="question">
							<div class="heard_img right">
								<img class="head" height="40"/>
							</div>
							<div class="question_text">
								`+ text + `
							<i></i>
						</div>
						</div>`);
				} else {
					li = $(`<div class="answer">
							<div class="heard_img left">
								<img class="head" height="40"/>
							</div>
							<div class="answer_text">
								`+ text + `
								<i></i>
						</div>
					</div>`);
				}
				li.attr('msgid', id);
				if (!$('[msgid=' + id + ']')[0]) {
					$('#scrollDiv').append(li);
				}
				MC.user.getInfo(uid, (json: any) => {
					li.find('.head').attr('src', json.head);
				});

				if (content.msg_type == 'image') {
					JIM.getResource({
						'media_id': content.msg_body.media_id,
					}).onSuccess(function (data:any) {
						li.find('[media_id="'+content.msg_body.media_id+'"]').attr('src',data.url);
						//data.code 返回码
						//data.message 描述
						//data.url 资源临时访问路径，具体超时时间expire time会包含在url中
					}).onFail(function (data:any) {
					 
					});
				}

			}


			/** 滚动到底部 */
			private buttom() {
				$(".speak_window").animate({ scrollTop: $(".speak_window").prop("scrollHeight") }, 400);
			}

		}

	}

}